<?php
class CategoriesController extends AppController {

	var $name = 'Categories';
	var $helpers = array('Html', 'Form');
	var $paginate=array(
	       'limit'=>'20',
	       'order'=>array('Category.name'=>'asc')
	   );
	var $uses = array('Category','Product');
	
	function beforeFilter(){
	   parent::beforeFilter();
	   $this->Auth->allow(array('view','index'));
	}   
	   
   
	   
	function admin_index(){
	    //$categories = $this->Category->find("all");
	    //$this->set(compact("categories"));
	    $this->set("categories", $this->paginate());
	}
	
	function index(){
	    $this->set("categories", $this->paginate());
	}
	
	function admin_add(){
	    if (!empty($this->data)) {
	    	$this->Category->create();
	    	if($this->Category->save($this->data)){
	    	    $this->Session->setFlash("The saving is ready!");
	    	    $this->redirect(array('action'=>'index'));//ezzel ugrik vissza az előző oldalra
	    	}
	    }	    
	}
	
	function admin_edit($id=null){
	    if(!$id && empty($this->data)){
	        $this->Session->setFlash("Category is invalid!");
	    	$this->redirect(array('action'=>'index'));//ezzel ugrik vissza az előző oldalra
	    }
	    if (!empty($this->data)) {
   	        if($this->Category->save($this->data)){
    	        $this->Session->setFlash("The saving is ready!");
    	        $this->redirect(array('action'=>'index'));//ezzel ugrik vissza az előző oldalra
            }
            else {
                $this->Session->setFlash("The saving not ready, plase check the data");
            }
        
	    }
	    else {
                $this->data = $this->Category->read(null,$id);
        }
	}
	
	function admin_view($id=null){
	    if(!$id){
	        $this->Session->setFlash("Category is invalid!");
	    	$this->redirect(array('action'=>'index'));//ezzel ugrik vissza az előző oldalra
	    }
	    $this->set('category',$this->Category->read(null,$id));
	}
	
	function admin_delete($id=null){
	    if(!$id){
	        $this->Session->setFlash("Category is invalid!");
	    	$this->redirect(array('action'=>'index'));//ezzel ugrik vissza az előző oldalra
	    }
	    if (!$this->Category->del($id)) {
	    	$this->Session->setFlash("Category not exists!");
	    } 
	    else{
	        $this->Session->setFlash("Category deleted!");
	    } 
	    $this->redirect(array('action'=>'index'));//ezzel ugrik vissza az előző oldalra
	}
	
	function view($url_name=0){
	    $this->Category->recursive= -1;
	    $category = $this->Category->findByUrlName($url_name); 
	    if(!$category){
	        $this->cakeError("error404");
	        return;
	    }
	    $products=$this->paginate("Product",array('category_id'=>$category['Category']['id']));
	    $this->set(compact("category","products"));
	}
}
?>